const crypto = require('crypto');

/**
 *  解析请求加密数据
 * @param r
 * @returns {*|{"44": number, "45": number, "46": number, "47": number, "48": number, "49": number, "50": number, "51": number, "52": number, "53": number, "10": number, "54": number, "11": number, "55": number, "12": number, "56": number, "13": number, "57": number, "14": number, "58": number, "15": number, "59": number, "16": number, "17": number, "18": number, "19": number, "0": number, "1": number, "2": number, "3": number, "4": number, "5": number, "6": number, "7": number, "8": number, "9": number, "60": number, "61": number, "62": number, "63": number, "20": number, "64": number, "21": number, "65": number, "22": number, "66": number, "23": number, "67": number, "24": number, "25": number, "26": number, "27": number, "28": number, "29": number, "30": number, "31": number, "32": number, "33": number, "34": number, "35": number, "36": number, "37": number, "38": number, "39": number, "40": number, "41": number, "42": number, "43": number}}
 */
function parseEncrypt(r) {
    var f = e1(e2(JSON.stringify(r.payload)))
        , p = sig(f);
    l.payload = f,
        l.sig = p
    return l
}

_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
    , _p = "W5D80NFZHAYB8EUI2T649RT2MNRMVE2O";
l = {
    "0": 0,
    "1": 0,
    "2": 0,
    "3": 0,
    "4": 0,
    "5": 0,
    "6": 0,
    "7": 0,
    "8": 0,
    "9": 0,
    "10": 0,
    "11": 0,
    "12": 0,
    "13": 0,
    "14": 0,
    "15": 0,
    "16": 0,
    "17": 0,
    "18": 0,
    "19": 0,
    "20": 0,
    "21": 0,
    "22": 0,
    "23": 0,
    "24": 0,
    "25": 0,
    "26": 0,
    "27": 0,
    "28": 0,
    "29": 0,
    "30": 0,
    "31": 0,
    "32": 0,
    "33": 0,
    "34": 0,
    "35": 0,
    "36": 0,
    "37": 0,
    "38": 0,
    "39": 0,
    "40": 0,
    "41": 0,
    "42": 0,
    "43": 0,
    "44": 0,
    "45": 0,
    "46": 0,
    "47": 0,
    "48": 0,
    "49": 0,
    "50": 0,
    "51": 0,
    "52": 0,
    "53": 0,
    "54": 0,
    "55": 0,
    "56": 0,
    "57": 0,
    "58": 0,
    "59": 0,
    "60": 0,
    "61": 0,
    "62": 0,
    "63": 0,
    "64": 0,
    "65": 0,
    "66": 0,
    "67": 0
}

function e1(t) {
    if (null == t)
        return null;
    for (var e, r, n, o, i, a, c, u = "", s = 0; s < t.length;)
        o = (e = t.charCodeAt(s++)) >> 2,
            i = (3 & e) << 4 | (r = t.charCodeAt(s++)) >> 4,
            a = (15 & r) << 2 | (n = t.charCodeAt(s++)) >> 6,
            c = 63 & n,
            isNaN(r) ? a = c = 64 : isNaN(n) && (c = 64),
            u = u + _keyStr.charAt(o) + _keyStr.charAt(i) + _keyStr.charAt(a) + _keyStr.charAt(c);
    return u
}

function e2(t) {
    for (var e = "", r = 0; r < t.length; r++) {
        var n = _p.charCodeAt(r % _p.length);
        e += String.fromCharCode(t.charCodeAt(r) ^ n)
    }
    return e
}

function md5(str) {
    const hash = crypto.createHash('md5');
    hash.update(str);
    return hash.digest('hex');
}

function sig(t) {
    return md5(t + _p).toUpperCase()
}


/**
 * 解析请求响应数据
 * @param l
 * @returns {any}
 */
function parseDecrypt(l) {
    var d = d1(l)
        , y = d2(d)
    , v = JSON.parse(y);

    return v
}

function d1(t) {

    var e, r, n, o, i, a, c = "", u = 0;
    for (t = t.replace(/[^A-Za-z0-9\+\/\=]/g, ""); u < t.length;) {
        e = _keyStr.indexOf(t.charAt(u++)) << 2 | (o = _keyStr.indexOf(t.charAt(u++))) >> 4,
            r = (15 & o) << 4 | (i = _keyStr.indexOf(t.charAt(u++))) >> 2,
            n = (3 & i) << 6 | (a = _keyStr.indexOf(t.charAt(u++))),
            c += String.fromCharCode(e),
        64 != i && (c += String.fromCharCode(r)),
        64 != a && (c += String.fromCharCode(n));
    }
    return c
}

function _u_d(t) {
    for (var e = "", r = 0, n = 0, o = 0, i = 0; r < t.length;)
        (n = t.charCodeAt(r)) < 128 ? (e += String.fromCharCode(n),
            r++) : n > 191 && n < 224 ? (o = t.charCodeAt(r + 1),
            e += String.fromCharCode((31 & n) << 6 | 63 & o),
            r += 2) : (o = t.charCodeAt(r + 1),
            i = t.charCodeAt(r + 2),
            e += String.fromCharCode((15 & n) << 12 | (63 & o) << 6 | 63 & i),
            r += 3);
    return e
}

function d2(t) {

    for (var e = "", r = 0; r < t.length; r++) {
        var n = _p.charCodeAt(r % _p.length);
        e += String.fromCharCode(t.charCodeAt(r) ^ n)
    }
    return e = _u_d(e)
}
